const router = require('koa-router')()

const Redis = require('ioredis')
const redis = new Redis.Cluster([
    {
        port: 7010,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7011,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7012,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7013,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7014,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7015,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7016,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7017,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7018,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7019,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7020,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7021,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7022,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7023,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7024,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7025,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7026,
        host: "127.0.0.1",
        password: "123654789",
    },
    {
        port: 7027,
        host: "127.0.0.1",
        password: "123654789",
    },

])

const timing = async (name = 'test', cb) => {
    console.time(name)
    typeof cb === 'function' && await cb()
    console.timeEnd(name)
}


router.prefix('/users')

router.get('/', function (ctx, next) {
    ctx.body = 'this is a users response!'
})

router.get('/bar/:testkey/:testvalue', function (ctx, next) {
    ctx.body = 'this is a users/bar response'
    console.log(ctx.params);
    // set
    timing('set', _ => {
        redis.set('foo', 'bar');
        redis.get('foo').then(res => {
            console.log(res);
        });
    });

// del
    timing('del', _ => {
        redis.del('foo').then(res => {
            console.log(res);
        });
    });

// sadd [set 集合]
    timing('sadd', _ => {
        redis.sadd('seta', 1, 3, 5, 7).then(res => console.log(res));
        redis.sadd('setb', [1, 3, 5, 7]).then(res => console.log(res));
    });

// lpush [list 列表]
    timing('lpush', _ => {
        redis.lpush('list', [1, 2, 3]).then(res => console.log(res));
        redis.lrange('list', [0, -1]).then(res => console.log(res));
    });

// 选择数据库 [序号]
    timing('select', _ => {
        // 选择 1 号数据库，然后查询 1 号数据库的 keys
        redis.select(1).then(res => {
            redis.set('name', 'xiaoming');
            redis.keys('*').then(res => console.log(res));
            console.log('select');
        })
    });

// // 事务
//     timing('basic', async () => {
//         redis.multi().set('a', 1).set('b', 2).exec();
//         console.log('multi');
//     });

// // 管道
//     timing('basic2', async () => {
//     redis.pipeline().get('a').get('b').exec().then(res => {
//         console.log(res);
//         console.log('pipe');
//     })
//     });
})

module.exports = router
